package com.wx.studentManage.controller;

import com.wx.studentManage.model.CodeTable;
import com.wx.studentManage.model.Page;
import com.wx.studentManage.model.User;
import com.wx.studentManage.service.CodeTableService;
import com.wx.studentManage.service.TeamService;
import com.wx.studentManage.service.UserService;
import com.wx.studentManage.util.MyDate;
import com.wx.studentManage.util.RandomValidateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private TeamService teamService;
    @Autowired
    private CodeTableService codeTableService;

    @PostMapping("/register")
    public HashMap register(@RequestBody User user){
        HashMap<String, String> map = new HashMap<>();
        String result = userService.register(user);
        map.put("result",result);
        return map;
    }

    @PostMapping("/login")
    public HashMap login(@RequestBody User user, HttpServletRequest request){
        //(k,v,ex)
        // v==code
        boolean b = RandomValidateCode.checkCode(user, request);
        Assert.isTrue(b,"验证码不对,验证码过期");
        HashMap<String, String> map = new HashMap<>();
        String result = userService.login(user,request);
        if ("1".equals(result)) {
            Jedis jedis = new Jedis();
            jedis.set("userCode",user.getUserCode());
        }
        map.put("result",result);
        return map;
    }

    @PostMapping("/loginRedis")
    public HashMap loginRedis(@RequestBody User user, HttpServletRequest request){
        //(k,v,ex)
        // v==code
        HashMap<String, String> map = new HashMap<>();
        Jedis jedis = new Jedis();
        String checkCode = jedis.get("checkCode");
        user.setCheckCode(checkCode);
        String result = userService.loginRedis(user,request);
        if ("1".equals(result)) {
            jedis.set("userCode",user.getUserCode());
        }
        map.put("result",result);
        return map;
    }

    @PostMapping("/perfect")
    public HashMap perfect(@RequestBody User user){
        HashMap<String, Integer> map = new HashMap<>();
        int result = userService.perfect(user);
        map.put("result",result);
        return map;
    }

    @GetMapping("/loginOut")
    public HashMap loginOut(){
        HashMap<String, Long> map = new HashMap<>();
        Jedis jedis = new Jedis();
        Long result = jedis.del("userCode");
        map.put("result", result);
        return map;
    }

    @GetMapping("/findUserByUserCode")
    public HashMap findUserByUserCode(@RequestParam String userCode){
        HashMap<String, Object> map = new HashMap<>();
        User user = userService.findUserByUserCode(userCode);
        map.put("data",user);
        return map;
    }

    @GetMapping("/getUserList")
    public HashMap getUserList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getUserList(data);
        int count = userService.getSelectCount(data);
        Page page = new Page();
        page.setList(users);
        page.setTotal(count);
        map.put("list",users);
        map.put("count",count);
        return map;
    }

    @PostMapping("/updateState")
    public HashMap updateState(@RequestBody User user){
        HashMap<String, Object> map = new HashMap<>();
        int result = userService.updateState(user);
        map.put("result",result);
        return map;
    }

    @GetMapping("/getUserByTeamCode")
    public HashMap getUserByTeamCode(@RequestParam String teamCode){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getUserByTeamCode(teamCode);
        List<CodeTable> codeTables = codeTableService.getCodeByType("sign_state");
        map.put("data",users);
        map.put("code",codeTables);
        return map;
    }

    @GetMapping("/getUserItemByNoTeamCode")
    public HashMap getUserItemByNoTeamCode(){
        HashMap<String, Object> map = new HashMap<>();
        List<User> users = userService.getUserItemByNoTeamCode();
        map.put("data",users);
        return map;
    }

    @PostMapping("/updateUserTeam")
    public HashMap updateUserTeam(@RequestParam String codeTeam,@RequestParam String teamCode,@RequestParam List<Integer> ids){
        HashMap<String, Object> map = new HashMap<>();
        String result = userService.updateUserTeam(teamCode,ids);
        if (result == "success"){
            int count = userService.getCountByTeam(codeTeam);
            Map param = new HashMap();
            param.put("teamSize",count);
            param.put("teamCode",codeTeam);
            param.put("updateTime", MyDate.getDate());
            teamService.updateMemberCount(param);
        }
        map.put("data",result);
        return map;
    }


}
